//
//  UploadRecordViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/31.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "UploadRecordViewController.h"
#import "ImagePickerManager.h"
#import "UploadImageViewController.h"
#import "MyCourseListViewModel.h"
@interface UploadRecordViewController ()
@property (nonatomic, weak)IBOutlet UIImageView *bgView;
@property (nonatomic, weak)IBOutlet UIButton *uploadResultBtn;
@property(nonatomic,weak)IBOutlet UIImageView *prdIcon;
@property(nonatomic,weak)IBOutlet UILabel *createTimeLab;
@property(nonatomic,weak)IBOutlet UILabel *useTimeLengthLab;
@property(nonatomic,weak)IBOutlet UILabel *startTimeLab;
@property(nonatomic,weak)IBOutlet UILabel *totalTimeLab;
@property(nonatomic,weak)IBOutlet UILabel *totalCountLab;
@property(nonatomic,weak)IBOutlet UILabel *feelLab;
@property(nonatomic,strong)UserUseListModel *model;
@property(nonatomic,strong)MyCourseListViewModel *courseViewModel;
@end

@implementation UploadRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"上传记录";
    [self.bgView setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    self.prdIcon.layer.cornerRadius = 23;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    [self.courseViewModel fetchUserDateList:[dateFormatter stringFromDate:[NSDate date]] cmp:^(NSArray * _Nonnull userUseList) {
        if ([userUseList count]>0) {
            self.model = [userUseList firstObject];
            [self setModelValue:self.model];
        }
    }];
}
- (void)leftbuttonPressed:(id)sender{
    [self.rt_navigationController popToRootViewControllerAnimated:YES complete:nil];
}
//-(IBAction)uploadPhotoBtnPressed:(id)sender{
//    [[ImagePickerManager sharedImgPicker]showImgActionWithType:ImagePickerTypeAll enableEditing:NO Completion:^(UIImage *usericon, UIImagePickerController *imgPicker) {
//        [self.uploadResultBtn setBackgroundImage:usericon forState:UIControlStateNormal];
//        [self.uploadResultBtn setImage:nil forState:UIControlStateNormal];
//        [self.uploadResultBtn setTitle:@"" forState:UIControlStateNormal];
//        [imgPicker dismissViewControllerAnimated:YES completion:nil];
//    }];
//}
-(IBAction)uploadImageBtnPressed:(id)sender{
    UploadImageViewController *uploadImage = [[UploadImageViewController alloc]init];
    uploadImage.model = self.model;
    [self.rt_navigationController pushViewController:uploadImage animated:YES complete:nil];
}
-(void)setModelValue:(UserUseListModel*)model{
    self.model = model;
    [self.prdIcon setImage:[UIImage imageNamed:@"icon_default"]];
    self.createTimeLab.text = [NSString stringWithFormat:@"%@",[self timestampSwitchTime:model.addTime/1000 andFormatter:@"yyyy.MM.dd"]];
    self.useTimeLengthLab.text = [NSString stringWithFormat:@"%.0f",model.useTimeLength];
    self.startTimeLab.text = [NSString stringWithFormat:@"%@",[self timestampSwitchTime:model.useTime/1000 andFormatter:@"hh:mm"]];
    self.totalTimeLab.text = @"暂无";
    self.totalCountLab.text  = @"1";
    if (!EC_ISNullStr(model.useFeel)) {
        self.feelLab.text = [NSString stringWithFormat:@"%@",model.useFeel];
    }else{
        self.feelLab.text = @"暂无";
    }
    if (!EC_ISNullStr(model.usePicture)) {
        [self.uploadResultBtn setImage:[UIImage new] forState:UIControlStateNormal];
        [self.uploadResultBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:model.usePicture] forState:UIControlStateNormal];
    }else{
        [self.uploadResultBtn setImage:[UIImage imageNamed:@"xiangji"] forState:UIControlStateNormal];
        [self.uploadResultBtn setBackgroundImage:[UIImage new] forState:UIControlStateNormal];
    }
}
#pragma mark - 将某个时间戳转化成 时间
-(NSString *)timestampSwitchTime:(double)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
    
}
#pragma mark - instance
-(MyCourseListViewModel*)courseViewModel{
    if (!_courseViewModel) {
        _courseViewModel = [[MyCourseListViewModel alloc]init];
    }
    return _courseViewModel;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
